package edu.hgnu.consumer;

import com.rabbitmq.client.*;
import edu.hgnu.utils.RabbitMQUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author:      Tara.Li(李春侣)
 * @ReWroteBy:  Tara.Li(李春侣)
 * @date:  2021-08-09 11:06:04
 * @Description:
 */
public class Consumer {
    public static void main(String[] args) throws IOException{
        //创建连接对象
        Connection connection = RabbitMQUtils.getConnection();
        //创建对象
        Channel channel = connection.createChannel();
        //通道绑定对象
        /**
         * queue:队列queue，
         * durable:是否持久化队列，false不持久化
         * b1:是否独占队列，true--是，false--否
         * b2:是否消费完毕自动删除队列，true--自动删除
         * map:额外附加参数
         */
        channel.queueDeclare("firstQueue",false,false,false,null);

        channel.basicConsume("firstQueue",true,new DefaultConsumer(channel){
            /**
             * @param consumerTag
             * @param envelope
             * @param properties
             * @param body
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                System.out.println("queue==="+new String(body));
            }
        });

        //关闭连接
        //RabbitMQUtils.closeConnAndChanel(connection,channel);
    }
}
